// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var footer = require("../footer/footer_gray");
	var top = require("../top/top_white");
	var flag = false;
	var tools = require("tools");
	var code = "";
	var init = function() {
		footer.init();
		top.init("登录");
		$("#head").removeClass("none");
		$("#main").removeClass("none");
		$("#footer").removeClass("none");
		router.init();
		initEvent();
		code = tools.createValidateCode($("#validateCode")).toLowerCase();
	};

	var initEvent = function() {
		//添加跳转
		base.clearData();

		$("#register").attr("href", "../register/register_main.html");
		$("#retrievePassword").attr("href", "../retrievePassword/retrieve_main.html");

		/*失去焦点时验证是否手机号码*/
		$("#main").on("blur", "#mobile", function() {
			var phoneNum = $("#mobile").val();
			if (phoneNum == null || phoneNum == "") {
				$('.error_info').css('visibility', 'visible');
				$(".error_info").html("请输入手机号码！");
			} else if (!validatePhoneNum(phoneNum)) {
				$('.error_info').css('visibility', 'visible');
				$(".error_info").html("请输入正确的手机号码！");
				flag = false;
			} else {
				$('.error_info').css('visibility', 'hidden');
				flag = true;
			}
		});

		/*失去焦点时验证是否输入密码*/
		$("#main").on("blur", "#password", function() {
			var password = $("#password").val();
			if (password == null || password == "") {
				$('.error_info').css('visibility', 'visible');
				$(".error_info").html("请输入密码！");
				flag = false;
			} else {
				$('.error_info').css('visibility', 'hidden');
				flag = true;
			}
		});

		/*重新获取验证码*/
		$("#main").on("click", "#validateCode", function() {
			code = tools.createValidateCode($("#validateCode"));
		});

		/*失去焦点时验证是否输入验证码*/
		$("#main").on("blur", "#validate", function() {
			var validate = $("#validate").val().toLowerCase();
			if (validate == null || validate == "") {
				$('.error_info').css('visibility', 'visible');
				$(".error_info").html("请输入验证码！");
				flag = false;
			} else if (validate != code) {
				$('.error_info').css('visibility', 'visible');
				$(".error_info").html("验证码错误！");
				flag = false;
			} else {
				$('.error_info').css('visibility', 'hidden');
				flag = true;
			}
		});

		//绑定登陆点击事件
		$("#main").on("click", ".login_button", function() {
			if (flag) {
				tools.protectDoubleClick();
				login();
			}
		});

		document.onkeydown = function(event) {
			if (13 == event.keyCode) {
				login();
			}
		};
	};

	//登陆
	var login = function() {
		var validate = $("#validate").val().toLowerCase();
		if (validate == null || validate == "") {
			$('.error_info').css('visibility', 'visible');
			$(".error_info").html("请输入验证码！");
			flag = false;
		} else if (validate != code) {
			$('.error_info').css('visibility', 'visible');
			$(".error_info").html("验证码错误！");
			flag = false;
		} else {
			$('.error_info').css('visibility', 'hidden');
			var mUsername = $("#mobile").val();
			var mPassword = $("#password").val();
			base.fnAjax({
				action: "userNameLogin.json",
				async: true,
				nocode: true,
				oData: {
					username: mUsername,
					password: mPassword,
					appKey: "FINANCE-ENTERPRISE-WEB"
				}, //传递参数
				fnSuccess: function(data) {
					tools.cancelProtectDoubleClick();
					if (data.status == 200) {
						base.saveData("sid", data.datas[0].sessionUuid); //存sessionUuid
						base.saveData("appKey", "FINANCE-ENTERPRISE-WEB"); //存appkey
						base.saveData("tenantId", data.datas[0].tenantId); //存accountId
						base.saveData("userName", mUsername.substr(0, mUsername));//存账号
						location.href = "../account/account.html";
					} else {
						//                  base.alert(2,"信息获取失败");
						$('.error_info').css('visibility', 'visible');
						$(".error_info").html(data.message);
					}
				}
			});
		}
	};

	//验证手机号码是否正确
	var validatePhoneNum = function(str) {
		var regMobile = /^0?1[3|4|5|7|8][0-9]\d{8}$/ //手机
		if (regMobile.test(str)) {
			return true;
		} else {
			return false;
		}
	}

	return {
		init: init
	};
});